function openModal (item) {

    var modalWindow = $("<div id='modalWindow'></div>");
    var modalContent = $("<div id='modalContent'></div>");
    var modalBackground = $("<div id='modalBackground'></div>");
    var modalCloseIcon = $("<img id='modalCloseIcon' src='/media/static/admin/img/admin/icon_deletelink.gif' />");
    modalBackground.fadeIn("fast");
    modalBackground.append(modalWindow);
    modalWindow.append(modalCloseIcon);
    $('#container').before(modalBackground);

    var modal = {
        'window': modalWindow,
        'content' : modalContent,
        'background' : modalBackground,
        'closeIcon' : modalCloseIcon
    };

    return modal;
}

function closeModal(modal) {
    $(modal).parent().parent().fadeOut("fast");
    $(modal).parent().parent().queue(function () {
        $(this).remove();
        $(this).dequeue();
    });
}

function showAddAnotherModal(triggeringLink) {
    modal = openModal(triggeringLink);
    var name = triggeringLink.id.replace(/^add_/, '');
    nameModal = id_to_windowname(name);
    href = triggeringLink.href;

    if (href.indexOf('?') == -1) {
        href += '?_popup=1';
    } else {
        href  += '&_popup=1';
    }

    $.ajax({
        url: href,
        beforeSend: function() {
            var loadingIcon = $("<img id='loadingIcon' src='/media/imagem/loading.gif'>");
            modal.content.html(loadingIcon);
            modal.window.append(modal.content);
        },
        success: function(data) {
            modal.content.html(data);
            var form = modal.content.find('form');
            form.addClass("loaded-ajax-form");
            modal.window.append(modal.content);
        }
    });

    return false;
}


function dismissAddAnotherModal(newId, newRepr) {
    // newId and newRepr are expected to have previously been escaped by
    // django.utils.html.escape.
    newId = html_unescape(newId);
    newRepr = html_unescape(newRepr);
    var name = windowname_to_id(nameModal);
    var elem = document.getElementById(name);
    if (elem) {
        if (elem.nodeName == 'SELECT') {
            var o = new Option(newRepr, newId);
            elem.options[elem.options.length] = o;
            o.selected = true;
        } else if (elem.nodeName == 'INPUT') {
            if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
                elem.value += ',' + newId;
            } else {
                elem.value = newId;
            }
        }
    } else {
        var toId = name + "_to";
        elem = document.getElementById(toId);
        var o = new Option(newRepr, newId);
        SelectBox.add_to_cache(toId, o);
        SelectBox.redisplay(toId);
    }
    closeModal(modal.closeIcon);
}




$(".loaded-ajax-form").live("submit", function(event) {
    event.preventDefault();


    ajaxFormParams = $(this).serializeArray();

    $.ajax({
        type: "POST",
        url: href,
        data: ajaxFormParams,
        success: function(data) {
            data = data.replace("opener.dismissAddAnotherPopup(window, ", "dismissAddAnotherModal(");
            $("#modalContent").html(data);
            $("#modalContent").find('form').addClass("loaded-ajax-form");

        }
    });

});